package com.imddy.spbatch001.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

//@Configuration
public class SecondDataSourceConfiguration {

    /**
     * @Author: xiongwei.dai
     * @Description 获取一级的属性
     * @Date 2023/04/04 16:48
     * @return org.springframework.boot.autoconfigure.jdbc.DataSourceProperties
     */

    @Bean("secondDataSourceProperties")
    @ConfigurationProperties("spring.datasource.second")
    public DataSourceProperties secondDataSourceProperties() {
        return new DataSourceProperties();
    }

    /**
     * @Author: xiongwei.dai
     * @Description 获取下一级的属性(hikari)并创建数据源
     * @Date 2023/04/04 16:48
     * @return com.zaxxer.hikari.HikariDataSource
     */

    @Bean("secondDataSource")
    @Qualifier(value = "secondDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.second.hikari")
    public HikariDataSource secondaryDataSource() {
        return secondDataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build();
    }

    /**
     * @Author: xiongwei.dai
     * @Description 配置事务管理器
     * @Date 2023/04/04 16:48
     * @param dataSource
     * @return org.springframework.transaction.PlatformTransactionManager
     */
//    @Bean(name = "secondTransactionManager")
//    public PlatformTransactionManager secondTransactionManager(@Qualifier("secondDataSource") DataSource dataSource) {
//        return new DataSourceTransactionManager(dataSource);
//    }


    /**
     * @Author: xiongwei.dai
     * @Description 创建JDBC模板
     * @Date 2023/04/04 16:48
     * @param dataSource
     * @return org.springframework.jdbc.core.JdbcTemplate
     */
    @Bean(name = "secondJdbcTemplate")
    public JdbcTemplate secondJdbcTemplate(@Qualifier("secondDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }



}
